package com.oliet.controller;


import com.oliet.common.dto.user.CreateUserDTO;
import com.oliet.common.dto.user.ResetPasswordDTO;
import com.oliet.common.dto.user.UpdateUserDTO;
import com.oliet.common.result.CommonResponse;
import com.oliet.common.vo.page.PageVO;
import com.oliet.common.vo.user.UserInfoVO;
import com.oliet.common.vo.user.UserVO;
import com.oliet.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author JZP
 * @since 2023-11-26
 */
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @GetMapping("/verifyUsername")
    public CommonResponse verifyUsername(@RequestParam String username) {
        this.usersService.verifyUsername(username);
        return CommonResponse.success();
    }

    @GetMapping("/verifyPassword")
    public CommonResponse verifyPassword(@RequestParam String username, @RequestParam String password) {
        this.usersService.verifyPassword(username, password);
        return CommonResponse.success();
    }


    @PostMapping("/create")
    public CommonResponse create(@Validated @RequestBody CreateUserDTO param) {
        this.usersService.create(param);
        return CommonResponse.success();
    }

    @PutMapping("/update")
    public CommonResponse update(@Validated @RequestBody UpdateUserDTO param) {
        this.usersService.update(param);
        return CommonResponse.success();
    }

    @GetMapping("/list")
    public CommonResponse list(@RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize,
                               @RequestParam(value = "search", required = false) String search) {
        PageVO<UserVO> result = this.usersService.pageList(pageNo, pageSize,search);
        return CommonResponse.data(result);
    }

    @PutMapping("/resetPassword")
    public CommonResponse resetPassword(@Validated @RequestBody ResetPasswordDTO param) {
        this.usersService.resetPassword(param);
        return CommonResponse.success();
    }

    @GetMapping("/info")
    public CommonResponse info(@RequestParam Long id) {
        UserInfoVO result = this.usersService.getInfo(id);
        return CommonResponse.data(result);
    }

    @GetMapping("/deleteById")
    public CommonResponse deleteById(@RequestParam Long id) {
        this.usersService.deleteById(id);
        return CommonResponse.success();
    }


}
